/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

myApp.controller('auth',
    function($scope, $location, loginService, $cookies, $rootScope) {
        $scope.name = "";
        $rootScope.user = {};
        $rootScope.user.name = "";
        $rootScope.showWelcomeMessage = false;
        $rootScope.showSearchBar = false;
        $rootScope.isLoginPage = false;
        
        $rootScope.showSearchBar = false;
        $scope.userLogin = function(){
            var loginSuccess = function(respData) {
                if(respData.data.name != "false") {
                    $rootScope.user = {};
                    $rootScope.user.name = respData.data.name;
                    $rootScope.storeVal("username", respData.data.name);
                    var cookie = {
                        name : $scope.userName,
                        type : 'user'
                    };
                    $cookies.userCookie = JSON.stringify(cookie); 
                    $location.path('/viewMeal');  
                } else {
                    $rootScope.showAlert("Please enter valid credentials.");
                }
            }
            var loginFailure = function(error) {
                $rootScope.showAlert("Please enter valid credentials.");
            }
            if($scope.userPassword == 'password') {
                loginService.isValidUser($scope.userName).then(loginSuccess, loginFailure);
            } else {
                $rootScope.showAlert("Please enter valid credentials.");
            }
        };
    
        $scope.chefLogin = function(){
            
            var loginSuccess = function(respData) {
                if(respData.data.name != "false") {
                    $rootScope.user = {};
                    $rootScope.user.name = respData.data.name;
                    $rootScope.storeVal("username", respData.data.name);
                    var cookie = {
                        name : $scope.chefUserName,
                        type : 'chef'
                    };
                    $cookies.userCookie = JSON.stringify(cookie); 
                    $location.path('/postmeal');  
                } else {
                    $rootScope.showAlert("Please enter valid credentials.");
                }
            }
            var loginFailure = function(error) {
                $rootScope.showAlert("Please enter valid credentials.");
            }
            
            if($scope.chefPassword == 'password') {
                loginService.isValidChef($scope.chefUserName).then(loginSuccess, loginFailure);
            } else {
                $rootScope.showAlert("Please enter valid credentials.");
            }
            
        };
    });
